package cn.stylefeng.guns.modular.business.service.impl;

import cn.stylefeng.guns.base.pojo.page.LayuiPageFactory;
import cn.stylefeng.guns.base.pojo.page.LayuiPageInfo;
import cn.stylefeng.guns.core.RecoverConst;
import cn.stylefeng.guns.modular.business.entity.AskedQuestion;
import cn.stylefeng.guns.modular.business.mapper.AskedQuestionMapper;
import cn.stylefeng.guns.modular.business.model.params.AskedQuestionParam;
import cn.stylefeng.guns.modular.business.model.result.AskedQuestionResult;
import  cn.stylefeng.guns.modular.business.service.AskedQuestionService;
import cn.stylefeng.roses.core.util.ToolUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import java.io.Serializable;
import java.util.Date;
import java.util.List;

/**
 * <p>
 * 常见问题 服务实现类
 * </p>
 *
 * @author pzp
 * @since 2019-07-29
 */
@Service
public class AskedQuestionServiceImpl extends ServiceImpl<AskedQuestionMapper, AskedQuestion> implements AskedQuestionService {

    @Override
    public void add(AskedQuestionParam param){
        AskedQuestion entity = getEntity(param);
        entity.setCreateTime(new Date());
        entity.setUpdateTime(new Date());
        entity.setDeleteFlag(RecoverConst.DEL_FLAG_FALSE);
        this.save(entity);
    }

    @Override
    public void delete(AskedQuestionParam param){
        this.removeById(getKey(param));
    }

    @Override
    public void update(AskedQuestionParam param){
        AskedQuestion oldEntity = getOldEntity(param);
        AskedQuestion newEntity = getEntity(param);
        ToolUtil.copyProperties(newEntity, oldEntity);
        newEntity.setUpdateTime(new Date());
        this.updateById(newEntity);
    }

    @Override
    public AskedQuestionResult findBySpec(AskedQuestionParam param){
        return null;
    }

    @Override
    public List<AskedQuestionResult> findListBySpec(AskedQuestionParam param){
        return null;
    }

    @Override
    public LayuiPageInfo findPageBySpec(AskedQuestionParam param,String condition){
        Page pageContext = getPageContext();
        IPage page = this.baseMapper.customPageList(pageContext, param,condition);
        return LayuiPageFactory.createPageInfo(page);
    }

    private Serializable getKey(AskedQuestionParam param){
        return param.getQuestionId();
    }

    private Page getPageContext() {
        return LayuiPageFactory.defaultPage();
    }

    private AskedQuestion getOldEntity(AskedQuestionParam param) {
        return this.getById(getKey(param));
    }

    private AskedQuestion getEntity(AskedQuestionParam param) {
        AskedQuestion entity = new AskedQuestion();
        ToolUtil.copyProperties(param, entity);
        return entity;
    }

    /**
     * 获取常见问题列表
     *
     * @param current 第几页
     * @param size    每页条数
     * @return
     */
    @Override
    public Page<AskedQuestionResult> get(Integer current, Integer size) {
        Page<AskedQuestionResult> page=new Page<>(current,size);
        return baseMapper.customPageList(page,new AskedQuestionParam(),"");
    }
}
